﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace BAL
{



    public partial class Form1 : Form
    {

        private MeasuerService.BarcodeJudgeServices bjs = new BAL.MeasuerService.BarcodeJudgeServices();
        private Socket isco = null;
        private string ipAddr = "192.168.30.203";
        int port = 5566;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            string measuer = "";
            string meavalue = "";

            //label6.Text = "制品编号" + bjs.GetKeycode(textBox1.Text.Trim());



        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            MeasuerMenu menuform = new MeasuerMenu();
            menuform.Show();
            this.Hide();
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {
            string measuer = "";
            string meavalue = "";
            isco = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress address = IPAddress.Parse(ipAddr);
            IPEndPoint point = new IPEndPoint(address, port);
            
                
                if (textBox1.Text.Trim().Length == 7)
                {
                    try
                    {
                        isco.Connect(point);
                        label8.Text = "OK";
                        label8.BackColor = Color.Green;
                        label4.Text = "判定:" + bjs.BalanceValueJudge(textBox1.Text.Trim(), DateTime.Now.ToString(), out measuer, out meavalue);
                        label3.Text = "包装码:" + textBox1.Text.Trim();
                        label5.Text = "扫描时间:" + DateTime.Now;
                        label6.Text = "BAL-REP基准:" + measuer + "kg.cm";
                        label2.Text = "测量值:" + meavalue + "N";
                        
                    }
                    catch
                    {
                        label8.Text = "NG";
                        label8.BackColor = Color.Red;
                        MessageBox.Show("请检查网络！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);           

                    }

                    textBox1.Text = "";
                    
                }


        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            isco = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPAddress address = IPAddress.Parse(ipAddr);
            IPEndPoint point = new IPEndPoint(address, port);
            try
            {
                isco.Connect(point);
                label8.Text = "OK";
                label8.BackColor = Color.Green;
            }
            catch
            {
                label8.Text = "NG";
                label8.BackColor = Color.Red;
            }
            textBox1.Focus();
        }
    }
}